package com.zhijian.warehouse.manage.service.impl;

import com.zhijian.warehouse.manage.dao.OperationalRealityMapper;
import com.zhijian.warehouse.manage.entity.po.OperationalReality;
import com.zhijian.warehouse.manage.entity.vo.request.OperationalRealitySearchVo;
import com.zhijian.warehouse.manage.entity.vo.request.OperationalRealitySaveOrUpdateVo;
import com.zhijian.warehouse.manage.entity.vo.response.OperationalRealityVo;
import com.zhijian.warehouse.manage.service.OperationalRealityService;
import com.zhijian.util.page.Page;
import com.zhijian.warehouse.manage.util.AssertCommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 操作实况业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-05-09 19:50:01
 */
@Slf4j
@Service
public class OperationalRealityServiceImpl implements OperationalRealityService {

	@Autowired
    private OperationalRealityMapper mapper;

    @Override
    public OperationalReality selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public OperationalRealityVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(OperationalReality operationalReality) {
        Date now = new Date();
        operationalReality.setCreateTime(now);
        operationalReality.setUpdateTime(now);
        return mapper.insert(operationalReality) > 0 ? operationalReality.getId() : 0L;
    }

    @Override
    public boolean update(OperationalReality operationalReality) {
        Date now = new Date();
        operationalReality.setUpdateTime(now);
        return mapper.updateByPrimaryKey(operationalReality) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(OperationalReality record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(OperationalRealitySaveOrUpdateVo saveOrUpdateVo) {
        OperationalReality operationalReality = new OperationalReality();
        BeanUtils.copyProperties(saveOrUpdateVo, operationalReality);
        return this.insert(operationalReality) != 0;
    }

    @Override
    public boolean modify(OperationalRealitySaveOrUpdateVo saveOrUpdateVo) {
        OperationalReality old = this.selectById(saveOrUpdateVo.getId());
        AssertCommonUtil.notNull(old, "此数据不存在或者已被删除");
        OperationalReality operationalReality = new OperationalReality();
        BeanUtils.copyProperties(saveOrUpdateVo, operationalReality);
        return this.update(operationalReality);
    }

    @Override
    public OperationalRealityVo selectOne(OperationalRealitySearchVo operationalRealitySearchVo) {
        return mapper.selectOne(operationalRealitySearchVo);
     }

    @Override
    public Integer count(OperationalRealitySearchVo operationalRealitySearchVo) {
        return mapper.count(operationalRealitySearchVo);
    }

    @Override
    public List<OperationalRealityVo> selectList(OperationalRealitySearchVo operationalRealitySearchVo) {
        return mapper.selectList(operationalRealitySearchVo);
    }

    @Override
    public Page<OperationalRealityVo> selectPage(OperationalRealitySearchVo operationalRealitySearchVo) {
        AssertCommonUtil.check(Objects.isNull(operationalRealitySearchVo.getPageNum()) || Objects.isNull(operationalRealitySearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(operationalRealitySearchVo);
        //分页
        Page<OperationalRealityVo> page = new Page<>(true, count, operationalRealitySearchVo.getPageNum(), operationalRealitySearchVo.getPageSize());
        operationalRealitySearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<OperationalRealityVo> responseVos = this.selectList(operationalRealitySearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<OperationalReality> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(OperationalRealitySearchVo operationalRealitySearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(operationalRealitySearchVo);
    }
    
}